package com.action.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.beans.user.UserInfo;
import com.business.user.UserInfoBus;
import com.config.SystemGlobals;
import com.jspsmart.upload.SmartUpload;
import com.util.MD5;
import com.util.TextTools;

public class UpdateUserInfo extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String contentType = SystemGlobals.getValue("contentType");
		PrintWriter out = response.getWriter();
		response.setContentType(contentType);
		MD5 md5 = new MD5();
		UserInfo ui = new UserInfo();
	
				String userPhotourl="";
		Log log = LogFactory.getLog(Register.class);
		SmartUpload SmartUpload = new SmartUpload();
		SmartUpload.initialize(getServletConfig(), request, response);
		SmartUpload.setTotalMaxFileSize(0xA00000L);
		try {
			SmartUpload.upload();
		} catch (Exception ex) {
			log.error("smartUpload upload():" + ex.getMessage());
		}

		String startpath = SystemGlobals.getValue("file_upload_path");
		startpath=startpath+"/photo";
		String filepath = "";
		try {
			for (int i = 0; i < SmartUpload.getFiles().getCount(); i++) {
				com.jspsmart.upload.File myfile = SmartUpload.getFiles()
						.getFile(i);
				String imagefile = myfile.getFileName();
				if (!myfile.isMissing()) {
					String md5str = md5.getMD5ofStr(myfile.getContentString());
					imagefile = md5str + "." + myfile.getFileExt();
					myfile.saveAs(startpath
							+ TextTools.mkHashDir(startpath, md5str) + "/"
							+ imagefile);
					filepath = "/upload/photo"
							+ TextTools.mkHashDir(startpath, md5str) + "/"
							+ imagefile;
									}
			}
		} catch (Exception se) {
			log.error("the upload File" + filepath + " is Failed "
					+ se.toString());
		}
		userPhotourl = filepath;
		String userName =  SmartUpload.getRequest().getParameter("username");
		String password =  SmartUpload.getRequest().getParameter("password");
		if (password != null&&!password.equals("")) {
			password = md5.getMD5ofStr(password);
			ui.setPassword(password);
		}
		String nickName =  SmartUpload.getRequest().getParameter("nickname");
		String email =  SmartUpload.getRequest().getParameter("email");
		String qq =  SmartUpload.getRequest().getParameter("qq");
		String msn =  SmartUpload.getRequest().getParameter("msn");
		String telephone =  SmartUpload.getRequest().getParameter("telephone");
		String mobile =  SmartUpload.getRequest().getParameter("mobile");
		String address =  SmartUpload.getRequest().getParameter("address");
		String province =  SmartUpload.getRequest().getParameter("Province");
		String city =  SmartUpload.getRequest().getParameter("City");
		String sex =  SmartUpload.getRequest().getParameter("sex");
		String birthday =  SmartUpload.getRequest().getParameter("birthday");
		String homepage =  SmartUpload.getRequest().getParameter("homepage");
		String gonggao =  SmartUpload.getRequest().getParameter("gonggao");
		String blogName =  SmartUpload.getRequest().getParameter("blogname");
		String ask =  SmartUpload.getRequest().getParameter("ask");
		String answer =  SmartUpload.getRequest().getParameter("answer");
		String lastIp =  request.getRemoteAddr();
		String blogMemo =  SmartUpload.getRequest().getParameter("blogmemo");
		String userIntro= SmartUpload.getRequest().getParameter("userintro");
		String isHide= SmartUpload.getRequest().getParameter("ishide");
		if(isHide==null){
			isHide="0";
		}
		String userLove= SmartUpload.getRequest().getParameter("userlove");

		ui.setBlogMemo(blogMemo);
		ui.setUsername(userName);
		ui.setUserHomepage(homepage);
		ui.setUserNickname(nickName);
		ui.setUserEmail(email);
		ui.setUserqq(qq);
		ui.setAddress(address);
		ui.setAnswer(answer);
		ui.setAsk(ask);
		ui.setBirthday(birthday);
		ui.setBlogName(blogName);
		ui.setCity(city);
		gonggao = TextTools.txt2html(gonggao);
		ui.setGonggao(gonggao);
		ui.setIsOpen("1");
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ui.setLastloginTime(sdf.format(date));
		ui.setMobile(mobile);
		ui.setMsn(msn);
		ui.setProvince(province);
		ui.setSex(sex);
		ui.setTelephone(telephone);
		ui.setUserLastIp(lastIp);
		ui.setIsHide(isHide);
		ui.setUserIntro(userIntro);
		ui.setUserLove(userLove);
		if(userPhotourl.equals("")){
			 userPhotourl =  SmartUpload.getRequest().getParameter("userphotourl");
		}
		ui.setUserPhotourl(userPhotourl);
		UserInfoBus uib = new UserInfoBus();
		uib.update(ui, userName);
		out.println("<script>alert(\"您成功地更新了您的个人信息\"); window.location=\"manage/"
				+ userName + "\";</script>");
	}
}
